<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/pages/public.js"></script>
    <link rel="import" href="../../common/common.html"  />
    <!--引入ckeditor-->
    <script src="../js/jquery-2.1.1.min.js"></script>
    <script src="../js/ckeditor/ckeditor.js"></script>
    <link href="../js/ckeditor/contents.css" rel="stylesheet">

    <!--引入bootstrap-->
    <link href="../js/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <script src="../js/bootstrap/js/bootstrap.min.js"></script>

    <!--引入bootstrap-treeview-->
    <link href="../js/bootstrap-treeview/dist/bootstrap-treeview.min.css" rel="stylesheet">
    <script src="../js/bootstrap-treeview/dist/bootstrap-treeview.min.js"></script>

    <!--引入bootStrap-addTabs-->
    <link href="../js/bootStrap-addTabs/bootstrap.addtabs.css" rel="stylesheet">
    <script src="../js/bootStrap-addTabs/bootstrap.addtabs.min.js"></script>

    <!--引入bootStrap-toastr-->
    <link href="../js/toastr/toastr.css" rel="stylesheet">
    <script src="../js/toastr/toastr.min.js"></script>

    <!--    bootstrap-table-->
    <link href="../js/bootstrap-table/dist/bootstrap-table.css" rel="stylesheet">
    <script src="../js/bootstrap-table/dist/bootstrap-table.js"></script>
    <script src="../js/bootstrap-table/dist/locale/bootstrap-table-zh-CN.js"></script><!--汉化文件-->

    <!--    bootstrap-fileinput-->
    <link href="../js/bootstrap-fileinput/css/fileinput.css" rel="stylesheet">
    <script src="../js/bootstrap-fileinput/js/fileinput.js"></script>
    <script src="../js/bootstrap-fileinput/js/locales/zh.js"></script>

    <!--    bootstrap-dialog-->
    <link href="../js/bootstrap-fileinput/js/bootstrap-dialog/dist/css/bootstrap-dialog.css" rel="stylesheet">
    <script src="../js/bootstrap-fileinput/js/bootstrap-dialog/dist/js/bootstrap-dialog.js"></script>

    <!--引入zTreev3-->
    <link href="../js/zTreev3/css/zTreeStyle/zTreeStyle.css" rel="stylesheet">
    <script src="../js/zTreev3/js/jquery.ztree.all.js"></script>
</head>
<body>


<ul id="treeDemo" class="ztree"></ul>
<script>
    var token =window.localStorage.getItem("token");
    var setting = {
        async:{
            headers:{"AutharizeToken":token},
            enable:true,
            url:domain+"/menu/BindMenu",
        },check:{
            enable:true
        },view : {
            addHoverDom: function(treeId, treeNode){
                console.log(treeNode)
                var  data =treeNode.data;
                console.log(data)
                var aObj = $("#" + treeNode.tId + "_a");
                if (treeNode.editNameFlag || $("#btnGroup"+treeNode.tId).length>0) return;
                var s = '<span id="btnGroup'+treeNode.tId+'">';
                if ( treeNode.level == 0 ) {
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="insert('+treeNode.id+')" href="#" >添加</a>';
                } else if ( treeNode.level == 1 ) {
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="update('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
                    if (treeNode.children == undefined) {
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteById('+treeNode.id+')" href="#" >删除</a>';
                    }
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="insert('+treeNode.id+')" href="#" >添加</a>';
                } else if ( treeNode.level == 2 ) {
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="update('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteById('+treeNode.id+')" href="#">删除</a>';
                }
                s += '</span>';
                aObj.after(s);
            },
            removeHoverDom: function(treeId, treeNode){
                $("#btnGroup"+treeNode.tId).remove();
            }
        }
    };

    $(function(){
        $.fn.zTree.init($("#treeDemo"), setting);
    });

    //增加事件
    function insert(id) {
        BootstrapDialog.show({
            title: '添加信息',//标题
            message:$('<div></div>').load("/Menu/InsertById"),
            size:BootstrapDialog.SIZE_WIDE,
            buttons: [{
                label: '添加',
                action: function (dialog) {
                    $.getJSON("/Menu/InsertMenu",$("#insertMenu").serialize()+"&pid="+id, function (data) {
                        if(data>=1){
                            toastr.success("增加成功");
                            dialog.close();
                            $.fn.zTree.init($("#treeDemo"), setting);
                        }else{
                            toastr.success("增加失败");
                        }
                    });
                }
            }, {
                label: '关闭',
                action: function (dialog) {
                    dialog.close();
                }
            }]
        });
    }

    //修改事件
    function update(ids) {
        BootstrapDialog.show({
            title: '修改',//标题
            message:$('<div></div>').load("/Menu/UpdateById?id="+ids),
            size:BootstrapDialog.SIZE_WIDE,
            buttons: [{
                label: '确定',
                action: function (dialog) {
                    $.getJSON("/Menu/UpdateMenu",$("#UpdateMenu").serialize(), function (data) {
                        if(data){
                            toastr.success("更新成功");
                            dialog.close();
                            $.fn.zTree.init($("#treeDemo"), setting);
                        }else{
                            toastr.error("更新失败");
                        }
                    })
                }
            }, {
                label: '关闭',
                action: function (dialog) {
                    dialog.close();
                }
            }]
        });
    }

    //删除事件
    function deleteById(pid){
        if (confirm("真的要删掉我嘛＞︿＜"))
            $.getJSON("/Menu/DeleteMenu", {id: pid}, function (data) {
                if (data) {
                    toastr.success("NICE!(❁´◡`❁)");
                    //刷新页面
                    $.fn.zTree.init($("#treeDemo"), setting);
                } else {
                    toastr.error("出大事了,删不掉了o(TヘTo)");
                }
            })
    }
</script>
</body>
</html>